package com.example.demolearn.designPatterns.agent;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.beans.Encoder;
import java.lang.reflect.Method;

public class CGLIBTest implements MethodInterceptor {
    private Object target;
    public Object getInstance(final Object object){
        this.target = object;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("你的电脑是什么型号");
        Object invoke = methodProxy.invokeSuper(o, objects);
        System.out.println("电脑关机");
        return invoke;
    }
}
